import requests
import pandas as pd
from lxml import etree
import wordcloud
import jieba

# bilibili视频弹幕的URL
url = "https://comment.bilibili.com/45119175.xml"
# 发送请求
response = requests.get(url)
xml = etree.fromstring(response.content)
# 解析数据
barrage = xml.xpath("/i/d/text()")
# 把列表转换成DataFrame
barrage_df = pd.DataFrame(barrage, columns=['弹幕内容'])
# 保存到本地
barrage_df.to_csv("弹幕.csv", encoding='utf_8_sig')

f=open('弹幕.csv',mode='r',encoding='utf-8')
text=f.read();
text_list=jieba.lcut(text)
string=' '.join(text_list)
w=wordcloud.WordCloud (
    font_path ="msyh.ttc",
    width= 800,
    height = 600, 
    max_words= 200, 
    background_color = 'white',
)
w.generate(string)
w.to_file('词云.jpg')